package com.gitee.huxing.course.domain.dao;

import com.gitee.huxing.course.domain.UserInfoDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.course.domain.dao
 * @ClassName: UserServiceImpl
 * @Author: huxing
 * @DateTime: 2021-09-05 上午10:06
 */
@Slf4j
@Service("userservice")
public class UserServiceImpl implements UserDAO{

    @Resource(name = "myConnection")
    Connection connection;

    @Override
    public List<UserInfoDO> list() {
        // 查询语句
        String sql = "select * from pl_user_info";
        // 查询列表记录
        final List<UserInfoDO> list = new ArrayList<>();
        try {
            PreparedStatement pre = connection.prepareStatement(sql);
            ResultSet resultSet =  pre.executeQuery();
            while (resultSet.next()){
                UserInfoDO userInfoDO = new UserInfoDO();
                userInfoDO.setUserId(resultSet.getString("user_id"));
                userInfoDO.setUserName(resultSet.getString("user_name"));
                userInfoDO.setUseTel(resultSet.getString("user_tel"));
                userInfoDO.setUseAddress(resultSet.getString("user_address"));
                userInfoDO.setCreateBy(resultSet.getString("create_by"));
                userInfoDO.setCreateDate(resultSet.getDate("create_date"));
                userInfoDO.setUpdateBy(resultSet.getString("update_by"));
                userInfoDO.setUpdateDate(resultSet.getDate("update_date"));
                userInfoDO.setDeleteFlag(resultSet.getInt("delete_flag"));
                userInfoDO.setRemark(resultSet.getString("remark"));
                // 添加结果集
                list.add(userInfoDO);
            }
        } catch (Exception ex){
            ex.printStackTrace();
        }
        // 检查返回的结果是否正确
        if (list != null && list.size() > 0){
            log.info("查询到的记录: {}", list.toString());
            return list;
        } else {
            return null;
        }
    }

    @Override
    public UserInfoDO selectByName(String userId) {
        // 查询语句
        String sql = "select * from pl_user_info where user_id = ?";
        UserInfoDO userInfoDO = new UserInfoDO();
        try {
            PreparedStatement pre = connection.prepareStatement(sql);
            // 设置第一个查询条件
            pre.setString(1, userId);
            // 执行查询处理
            pre.execute();
            ResultSet set = pre.getResultSet();
            if (set.next()){
                userInfoDO.setUserId(set.getString("user_id"));
                userInfoDO.setUserName(set.getString("user_name"));
                userInfoDO.setUseTel(set.getString("user_tel"));
                userInfoDO.setUseAddress(set.getString("user_address"));
                userInfoDO.setCreateBy(set.getString("create_by"));
                userInfoDO.setCreateDate(set.getDate("create_date"));
                userInfoDO.setUpdateBy(set.getString("update_by"));
                userInfoDO.setUpdateDate(set.getDate("update_date"));
                userInfoDO.setDeleteFlag(set.getInt("delete_flag"));
                userInfoDO.setRemark(set.getString("remark"));
                log.info("查询到的记录: {}", userInfoDO.toString());
            }

        } catch (Exception ex){
            ex.printStackTrace();
        }
        return userInfoDO;
    }

    @Override
    public void task(){
        long startTime = System.currentTimeMillis();
        try {
            // 查询语句
            String sql = "insert into t_sys_test(nums) values (?)";
            PreparedStatement pre = connection.prepareStatement(sql);
            // 设置第一个查询条件
            for (int i=0; i<1000; i++){
                for (int j=0; j<1000; j++){
                    pre.setString(1, "1");
                    pre.addBatch();
                }
                pre.executeBatch();
                pre.clearBatch();
            }
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
            long endTime = System.currentTimeMillis();
            System.out.println("总共耗时间: " + (endTime - startTime) + "ms");
        }
    }
}
